<script lang="ts" setup name="XtxCity">
export type cityItem = {
  code: string
  level: number
  name: string
  areaList: cityItem[]
}

// 选择的城市结果类型
export type CityResult = {
  provinceCode: string
  provinceName: string
  cityCode: string
  cityName: string
  countyCode: string
  countyName: string
}
import { ref, watchEffect } from 'vue'
import axios from 'axios'
import { onClickOutside } from '@vueuse/core'
const active = ref(false)
const cityList = ref<cityItem[]>([])
const cacheList = ref<cityItem[]>([])
const target = ref(null)

// 当目标元素外面点击触发的事件
onClickOutside(target, () => {
  active.value = false
})

async function getCityList() {
  const res = await axios.get<cityItem[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  cityList.value = res.data
  cacheList.value = res.data
}
getCityList()

defineProps<{
  address?: string
}>() 

const emit = defineEmits<{(e: 'changeCity', city: CityResult): void}>()

const cityResult = ref<CityResult>({} as CityResult)

const selectCity = (city: cityItem) => {
  // 选择城市时判断当前选择的是省还是市还是区
  if(city.level === 0) {
    cityResult.value.provinceName = city.name
    cityResult.value.provinceCode = city.code
  }
  if(city.level === 1) {
    cityResult.value.cityName = city.name
    cityResult.value.cityCode = city.code
  }
  if(city.level === 2) {
    cityResult.value.countyName = city.name
    cityResult.value.cityCode = city.code

    // 子传父
    emit('changeCity', cityResult.value)
  }
  if(!city.areaList) return active.value = false
  cityList.value = city.areaList
}

watchEffect(() => {
  if(!active.value) cityList.value = cacheList.value
})

</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="active = !active" :class="{ active }">
      <span class="placeholder" v-if="address">{{ address }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span 
      class="ellipsis"
      v-for="item in cityList"
      :key="item.name"
      @click="selectCity(item)"
      >{{ item.name }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>